#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 最新资料
爬取类目：最新股本状况/最新财务指标
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/lastest/szmb000001.html
"""
"""
result_sample_of_company_lastest = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'lstCaptialStock': {
        'totalShares',
        'circulatingShares',
        'stateShares',
        'legalShares',
        'sponsorShares',
        'transferredShares',
        'BShares',
        'HShares',
    },
    'lstFinancialIndex: {
        'EPS': '', #Earnings Per Share
        'ACPS': '', #Additional Paid-In Capital Per Share
        'UPPS': '', #Undistributed Profit Per Share
        'ROE': '', #Rate of Return on Common Stockholders' Equity
        'NAPS': '', #Net Asset Per Share
    }
}
"""

import sys

from company_crawler import CompanyCrawler
from company_crawler import CompanyInfoUtil
from xtls.timeparser import now
from bs4 import BeautifulSoup
from xtls.logger import get_logger

reload(sys)
sys.setdefaultencoding('utf-8')

CATEGORY = 'lastest'

DETAIL_MAPPING_CAPTIAL_STOCK = {
    u'总股本：': 'totalShares',
    u'流通股：': 'circulatingShares',
    u'国家股：': 'stateShares',
    u'法人股：': 'legalShares',
    u'发起人股：': 'sponsorShares',
    u'转配股：': 'transferredShares',
    u'B股：': 'BShares',
    u'H股：': 'HShares',
}

DETAIL_MAPPING_FINANCIAL_INDEX ={
    u'每股收益(元)：': 'EPS', #Earnings Per Share
    u'每股资本公积金(元)：': 'ACPS', #Additional Paid-In Capital Per Share
    u'每股未分配利润(元)：': 'UPPS', #Undistributed Profit Per Share
    u'净资产收益率(%)：': 'ROE', #Rate of Return on Common Stockholders' Equity
    u'每股净资产(元)：': 'NAPS', #Net Asset Per Share
}

class CompanyLast(CompanyCrawler):
    def __init__(self, company_info_util_instance):
        super(CompanyLast, self).__init__(company_info_util_instance=company_info_util_instance)
        self.detail_mapping = self.company_info.detail_mapping

    def find_company_info_from(self, url):
        result = {}
        html = self.get(url)
        soup = BeautifulSoup(html, 'html5lib')
        if not soup.find('img', src='/error.jpg'):
            tables = soup.select('div.zx_left div.clear')
            global DETAIL_MAPPING_CAPTIAL_STOCK
            global DETAIL_MAPPING_FINANCIAL_INDEX
            if len(tables) == 2:
                result['lstCaptialStock'] = self._find_lastest_info(tables[0])
                result['lstFinancialIndex'] = self._find_lastest_info(tables[1])
            else:
                self.logger.info(u'undealed: the page format has been changed %s' % url)
        else:
            self.logger.info(u'undealed: not find the company %s' % url)
        return result

    def _find_lastest_info(self, soup):
        lastest_info = {}
        tr_list = soup.find_all('tr')
        if tr_list:
            for tr in tr_list:
                tds = tr.find_all('td')
                if len(tds) != 2:
                    continue
                title = tds[0].getText()
                value = tds[1].getText()

                key = self.detail_mapping.get(title, None)
                if key:
                    lastest_info[key] = value
                else:
                    self.logger.info(u'undealed: undefined %s' % title)
        else:
            self.logger.info(u'undealed: the page format has been changed--No Table')
        return lastest_info

    def save(self, data):
        data['updateTime'] = now()
        for key, value in data.items():
            if key in ['lstCaptialStock', 'lstFinancialIndex']:
                for child_key, child_value in value.items():
                    print '{}: {}'.format(child_key, child_value)
                print '-'*40
            else:
                print '{}: {}'.format(key, value)

        print "=" * 70

    def deal(self, tp, soup):
        super(CompanyLast, self).deal(tp=tp, soup=soup)

    def run(self, startType=1, endType=4):
        super(CompanyLast, self).run(startType=startType, endType=endType)

def _merge_mapping():
    global DETAIL_MAPPING_FINANCIAL_INDEX
    global DETAIL_MAPPING_CAPTIAL_STOCK
    temp_dict = {}
    temp_dict.update(DETAIL_MAPPING_CAPTIAL_STOCK)
    temp_dict.update(DETAIL_MAPPING_FINANCIAL_INDEX)
    return temp_dict

def main():
    global CATEGORY
    from_company_info = CompanyInfoUtil(category=CATEGORY, detail_mapping=_merge_mapping())
    from_company_info.set_logger(get_logger(__file__))
    try:
        CompanyLast(from_company_info).run()
    except:
        from_company_info.logger.warn('%s shuttdown' % from_company_info.category)

if __name__ == '__main__':
    main()
